package torres.item;

import java.awt.image.BufferedImage;

import torres.entity.Entity;
import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;
import torres.fileIO.Saveable;
import torres.view.Displayable;
import torres.view.ImageLoader;

/**
 * @author Torres
 * Represents a base Item, providing a skeleton for the
 * specific kinds of items.
 */
public abstract class Item implements Displayable, Saveable
{
	private String name;
	private int id;
	private boolean consumed = false;
	
	private ImageLoader loader;
	private BufferedImage image;
	
	protected Item(String name, int id) {
		this.setName(name);
		this.setID(id);
	}
	
	/**
	 * Triggers the effect of an Entity walking on this item.
	 * @param e The Entity that is walking on this item
	 */
	public abstract void onTouch(Entity entity);
	
	/**
	 * Returns the name of the item.
	 * @return The name of the item.
	 */
	protected String getName() {
		
		return this.name;
	}
	
	/**
	 * Set the name of the item to the specified name.
	 * @param The new name of the item.
	 */
	protected void setName(String name) {
		
		this.name = name;
	}
	
	/* (non-Javadoc)
	 * @see torres.view.Displayable#getImage()
	 */
	public BufferedImage getImage() {
		
		if(image == null) {
			
			loader = new ImageLoader(name + ".png");
			image = loader.getImage();
		}
		
		return image;
	}

	/**
	 * Get the name of the image associated with this Item.
	 * @return The name of the image. Equivalent to this.getName() + ".png".
	 */
	public String getImageName() {
		return this.getName() + ".png";
	}
	
	/**
	 * Returns the String representation of this Item.
	 * @return The String representation of this Item.
	 */
	@Override
	public String toString() {
		
		return this.getName();
	}
	
	
	/**
	 * Tests to see if this Item is equivalent to the specified Item.
	 * @param The item to be tested for equality.
	 * @return True if this Item has the same name as the specified Item.
	 */
	public boolean equals(Item item) {
		return this.getName().equals(item.getName());
	}

	/**
	 * Sets the consumer flag of this item to the specified boolean.
	 * @param consumed The new value of consumed.
	 */
	public void setConsumed() {
		this.consumed = true;
	}

	/**
	 * Tests to see if this item was consumed.
	 * @return True if this Item was consumed on last touch, false otherwise.
	 */
	public boolean isConsumed() {
		
		return consumed;
	}
	
	/* (non-Javadoc)
	 * @see torres.fileIO.Saveable#save()
	 */
	@Override
	public ClassContainer save() {
		ClassContainer curClass = new ClassContainer("Item");
		curClass.addProperty(new BasicContainer("id", id));
		
		return curClass;
	}

	/* (non-Javadoc)
	 * @see torres.fileIO.Saveable#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer classContainer) {
		BasicContainer idCont = (BasicContainer)classContainer.getProperty("id");
		id = Integer.parseInt((String)idCont.getValue());
		
	}
	
	/**
	 * Sets Item's ID.
	 * @param id new ID value to set
	 */
	public void setID(int id) {
		this.id = id;
	}
	
	/**
	 * Return Item's ID
	 * @return id value of item
	 */
	public int getID() {
		return this.id;
	}
}
